// Written by Steve Andersen, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/
public class ProjectMaintenance {
	private Map<Id,Opportunity> ProjectMap = new Map<Id,Opportunity>();
	public List<ONEN_Project_Task__c> TasksInProjects = new List<ONEN_Project_Task__c>();
	public List<Project_Invoice__c> InvoicesInProjects = new List<Project_Invoice__c>();
	public List<Project_Consulting_Fee__c> ConsultingFeesInProjects = new List<Project_Consulting_Fee__c>();
	public List<Restricted_Fund_for_Project__c> RestrictedFundsInProjects = new List<Restricted_Fund_for_Project__c>();
	
	public ProjectMaintenance(Map<Id,Opportunity> TheseProjects) {
		//get the passed in map
		ProjectMap = TheseProjects;
		//system.debug('Map as passed in to HH object: '+HouseholdsMap);
		//create a list of Contacts that are members of that Household
		if(ProjectMap.size()>0) {
			TasksInProjects = [Select Worked_Hours_New__c, Expected_Hours__c,Opportunity__c from ONEN_Project_Task__c where Opportunity__c IN : ProjectMap.keySet() order by Opportunity__c];
			InvoicesInProjects = [Select Id, Amount__c,Opportunity__c from Project_Invoice__c where Opportunity__c IN : ProjectMap.keySet() order by Opportunity__c];
			ConsultingFeesInProjects = [Select Id, Amount__c,Opportunity__c from Project_Consulting_Fee__c where Opportunity__c IN : ProjectMap.keySet() order by Opportunity__c];
			RestrictedFundsInProjects = [Select Id, Amount__c,Opportunity__c from Restricted_Fund_for_Project__c where Opportunity__c IN : ProjectMap.keySet() order by Opportunity__c];
		
		}
	}
	
	public void reCalcluateHours() {
		Id PreviousOppId;
        Id CurrentOppId;
        Double ExpectedHoursTotal;
        Double WorkedHoursTotal;
        ExpectedHoursTotal=0;
        WorkedHoursTotal=0;
        
        PreviousOppId=null;
        
        for(Opportunity ProjectToZeroOut : ProjectMap.values()) {
        	ProjectToZeroOut.Worked_Hours__c=0;
        	ProjectToZeroOut.Scheduled_Hours__c=0;
        }
        
        for (ONEN_Project_Task__c TasksToRollup : TasksInProjects) {
        	Opportunity thisOpp;
            CurrentOppId = TasksToRollup.Opportunity__c ;            
			thisOpp = ProjectMap.get(CurrentOppId);		
            if(CurrentOppId!=PreviousOppId) {
                //add the map
                if (PreviousOppId!=null) {
                    thisOpp.Worked_Hours__c = WorkedHoursTotal;
                    thisOpp.Scheduled_Hours__c = ExpectedHoursTotal;
                    ExpectedHoursTotal=0;
                    WorkedHoursTotal=0;
                    
                }

                //start a new map
                //system.debug('get here? '+ TasksToRollup.Expected_Hours__c);
                if(TasksToRollup.Worked_Hours_New__c!=null) {
                	WorkedHoursTotal = TasksToRollup.Worked_Hours_New__c;
                }
                if(TasksToRollup.Expected_Hours__c!=null) {
                	ExpectedHoursTotal = TasksToRollup.Expected_Hours__c;
                }
                PreviousOppId = CurrentOppId;
            } else {
                //add a new element to the map
                if(TasksToRollup.Expected_Hours__c!=null) {
                	ExpectedHoursTotal+= TasksToRollup.Expected_Hours__c;
                }
                if(TasksToRollup.Worked_Hours_New__c!=null) {
                	WorkedHoursTotal+=TasksToRollup.Worked_Hours_New__c;
                }
            }  
            if(TasksToRollup.Worked_Hours_New__c!=null) {
            	thisOpp.Worked_Hours__c = WorkedHoursTotal;
            }
            if(TasksToRollup.Expected_Hours__c!=null) {
            	thisOpp.Scheduled_Hours__c = ExpectedHoursTotal; 
            }         
                
        }
	}
	public void reCalcluateInvoices() {
		Id PreviousOppId;
        Id CurrentOppId;
        Double InvoiceTotal;
        InvoiceTotal=0;
                
        PreviousOppId=null;
        
        for(Opportunity ProjectToZeroOut : ProjectMap.values()) {
        	ProjectToZeroOut.Invoiced_Amount__c=0;
        }
       //system.debug('invoices: '+InvoicesInProjects);
        for (Project_Invoice__c InvoicesToRollup : InvoicesInProjects) {
        	Opportunity thisOpp;
            CurrentOppId = InvoicesToRollup.Opportunity__c ;            
			thisOpp = ProjectMap.get(CurrentOppId);		
            if(CurrentOppId!=PreviousOppId) {
                //add the map
                if (PreviousOppId!=null) {
                	thisOpp.Invoiced_Amount__c = InvoiceTotal;
                	
                    InvoiceTotal=0;
                    
                }

                //start a new map
               // system.debug('get here? '+ TasksToRollup.Expected_Hours__c);
                if(InvoicesToRollup.Amount__c!=null) {
                	InvoiceTotal = InvoicesToRollup.Amount__c;
                }
                PreviousOppId = CurrentOppId;
            } else {
                //add a new element to the map
                if(InvoicesToRollup.Amount__c!=null) {
                	InvoiceTotal+= InvoicesToRollup.Amount__c;
                }
                
            }  
            thisOpp.Invoiced_Amount__c = InvoiceTotal;          
                
        }
	}
	public void reCalcluateConsultantFees() {
		Id PreviousOppId;
        Id CurrentOppId;
        Double ConsultantFeeTotal;
        ConsultantFeeTotal=0;
                
        PreviousOppId=null;
        
        for(Opportunity ProjectToZeroOut : ProjectMap.values()) {
        	ProjectToZeroOut.Fees_to_Consultants__c=0;
        }
      // system.debug('invoices: '+InvoicesInProjects);
        for (Project_Consulting_Fee__c ConsultingFee : ConsultingFeesInProjects) {
        	Opportunity thisOpp;
            CurrentOppId = ConsultingFee.Opportunity__c ;            
			thisOpp = ProjectMap.get(CurrentOppId);		
            if(CurrentOppId!=PreviousOppId) {
                //add the map
                if (PreviousOppId!=null) {
                    thisOpp.Fees_to_Consultants__c = ConsultantFeeTotal;
                    ConsultantFeeTotal=0;
                    
                }

                //start a new map
               // system.debug('get here? '+ TasksToRollup.Expected_Hours__c);
				if(ConsultingFee.Amount__c!=null) {
					ConsultantFeeTotal = ConsultingFee.Amount__c;
				}
                PreviousOppId = CurrentOppId;
            } else {
                //add a new element to the map
                if(ConsultingFee.Amount__c!=null) {
                	ConsultantFeeTotal+= ConsultingFee.Amount__c;
                }
                
            }  
            thisOpp.Fees_to_Consultants__c = ConsultantFeeTotal;          
                
        }
	}
	public void reCalcluateProjectFunds() {
		
		Id PreviousOppId;
        Id CurrentOppId;
        Double RetrictedFundTotal;
        RetrictedFundTotal=0;
                
        PreviousOppId=null;
        
        for(Opportunity ProjectToZeroOut : ProjectMap.values()) {
        	ProjectToZeroOut.Total_Restricted_Funds__c=0;
        }
      // system.debug('invoices: '+InvoicesInProjects);
        for (Restricted_Fund_for_Project__c RestrictedFundForProject : RestrictedFundsInProjects) {
        	Opportunity thisOpp;
            CurrentOppId = RestrictedFundForProject.Opportunity__c ;            
			thisOpp = ProjectMap.get(CurrentOppId);		
            if(CurrentOppId!=PreviousOppId) {
                //add the map
                if (PreviousOppId!=null) {
                    thisOpp.Total_Restricted_Funds__c = RetrictedFundTotal;
                    RetrictedFundTotal=0;
                    
                }

                //start a new map
               // system.debug('get here? '+ TasksToRollup.Expected_Hours__c);
				if(RestrictedFundForProject.Amount__c!=null) {
                	RetrictedFundTotal = RestrictedFundForProject.Amount__c;
				}
                PreviousOppId = CurrentOppId;
            } else {
                //add a new element to the map
                if(RestrictedFundForProject.Amount__c!=null) {
                	RetrictedFundTotal+= RestrictedFundForProject.Amount__c;
                }
            }  
            thisOpp.Total_Restricted_Funds__c = RetrictedFundTotal;          
                
        }
	}
}